/**
 * H.265/HEVC codec support detection utility
 */

export interface H265SupportResult {
  encoding: boolean;
  decoding: boolean;
  hardwareAcceleration: boolean;
  supportedProfiles: string[];
}

/**
 * Common H.265 codec configurations
 */
const H265_CODECS = [
  'hev1.1.6.L93.B0',      // Main Profile, Level 3.1
  'hev1.1.6.L120.B0',     // Main Profile, Level 4.0
  'hev1.1.6.L123.B0',     // Main Profile, Level 4.1
  'hvc1.1.6.L93.B0',      // Main Profile, Level 3.1 (hvc1 format)
  'hev1.2.4.L93.B0',      // Main 10 Profile, Level 3.1
];

/**
 * Check H.265 encoding support
 */
export async function checkH265EncodingSupport(
  width = 1280,
  height = 720,
  bitrate = 2_000_000,
  framerate = 30
): Promise<{ supported: boolean; codec?: string; hardwareAcceleration?: string }> {
  if (!('VideoEncoder' in window)) {
    return { supported: false };
  }

  for (const codec of H265_CODECS) {
    try {
      const config = {
        codec,
        width,
        height,
        bitrate,
        framerate,
        hardwareAcceleration: 'prefer-hardware' as const,
      };

      const result = await VideoEncoder.isConfigSupported(config);
      console.log('H.265 encoding support check for codec', codec, ':', result);
      if (result.supported) {
        return {
          supported: true,
          codec,
          hardwareAcceleration: result.config?.hardwareAcceleration,
        };
      }
    } catch (e) {
      // Continue to next codec
      continue;
    }
  }

  return { supported: false };
}

/**
 * Check H.265 decoding support
 */
export async function checkH265DecodingSupport(): Promise<{ supported: boolean; codec?: string }> {
  if (!('VideoDecoder' in window)) {
    return { supported: false };
  }

  for (const codec of H265_CODECS) {
    try {
      const config = {
        codec,
      };

      const result = await VideoDecoder.isConfigSupported(config);
      
      if (result.supported) {
        return {
          supported: true,
          codec,
        };
      }
    } catch (e) {
      continue;
    }
  }

  return { supported: false };
}

/**
 * Comprehensive H.265 support detection
 */
export async function checkH265Support(
  encodingConfig?: {
    width?: number;
    height?: number;
    bitrate?: number;
    framerate?: number;
  }
): Promise<H265SupportResult> {
  const [encodingResult, decodingResult] = await Promise.all([
    checkH265EncodingSupport(
      encodingConfig?.width,
      encodingConfig?.height,
      encodingConfig?.bitrate,
      encodingConfig?.framerate
    ),
    checkH265DecodingSupport(),
  ]);

  const supportedProfiles: string[] = [];
  
  // Collect all supported configurations
  for (const codec of H265_CODECS) {
    try {
      const encResult = await VideoEncoder.isConfigSupported({
        codec,
        width: 1280,
        height: 720,
        bitrate: 2_000_000,
        framerate: 30,
      });
      
      console.log('H.265 encoding support check for codec', codec, ':', encResult);
      if (encResult.supported) {
        supportedProfiles.push(codec);
      }
    } catch (e) {
      // Ignore errors
    }
  }

  return {
    encoding: encodingResult.supported,
    decoding: decodingResult.supported,
    hardwareAcceleration: encodingResult.hardwareAcceleration === 'prefer-hardware',
    supportedProfiles,
  };
}

/**
 * Simple boolean check - whether H.265 encoding is supported
 */
export async function isH265Supported(): Promise<boolean> {
  const result = await checkH265EncodingSupport();
  return result.supported;
}

/**
 * Get recommended H.265 codec string
 */
export async function getRecommendedH265Codec(): Promise<string | null> {
  const result = await checkH265EncodingSupport();
  return result.codec || null;
}

export default {
  checkH265Support,
  checkH265EncodingSupport,
  checkH265DecodingSupport,
  isH265Supported,
  getRecommendedH265Codec,
};